package org.pz.camunda.listener;

import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.delegate.DelegateTask;
import org.camunda.bpm.engine.delegate.TaskListener;
import org.pz.camunda.common.StringPoolUtil;
import org.pz.camunda.enums.OperateTypeEnum;
import org.pz.camunda.mapper.VariablesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;

/**
 * 请假流程：监听发起节点
 *  如果是首次发起，那么应该直接跳过发起节点
 *  如果是回退回来的，则不能跳过发起节点
 * */
@Slf4j
@Component("askForLevel_start")
public class AskForLevelStartListener implements TaskListener {

    @Autowired
    private TaskService taskService;

    @Autowired
    private VariablesMapper variablesMapper;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public void notify(DelegateTask delegateTask) {
        log.info("askForLevel_start：{}", delegateTask.getId());
        Boolean rejectFlag = (Boolean)delegateTask.getVariable(StringPoolUtil.REJECT_FLAG);
        //当未设置回退标志或者回退标志为 false 时，触发自动提交
        if(rejectFlag == null || !rejectFlag){

            //todo 先随便设置个 用户
            String userId = "admin";

            delegateTask.setVariable(StringPoolUtil.ASK_FOR_LEVEL_Personnel_Matters, userId);
            taskService.complete(delegateTask.getId());

            variablesMapper.createProcessTask(
                    delegateTask.getId(),
                    userId,
                    sdf.format(delegateTask.getCreateTime()),
                    OperateTypeEnum.START.getCode(),
                    OperateTypeEnum.START.getName());
        }


    }
}
